﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

using TrainingCenterSystem.Utils;

namespace TrainingCenterSystem.UserManagement
{
    public class UserFileReader
    {
        #region private members
        private XmlTextReader _xmlReader;
        private ArrayList _users;
        private List<Supervisor> _approvalList;
        private bool _isApproved = true;
        #endregion

        #region setters and getters
        public ArrayList Users
        {
            get { return _users; }
        }

        public List<Supervisor> ApprovalList
        {
            get { return _approvalList; }
        }
        #endregion

        #region constructor(s)
        public UserFileReader()
        {
        }
        #endregion

        #region methods
        public void read(string filename)
        {
            _xmlReader = new XmlTextReader(filename);

            while (_xmlReader.Read())
            {
                if ((_xmlReader.NodeType == XmlNodeType.Element) && (_xmlReader.Name == "SharpVTCUsers"))
                {
                    _isApproved = true;
                    this.createUserList();
                }
                else if ((_xmlReader.NodeType == XmlNodeType.Element) && (_xmlReader.Name == "Approval"))
                {
                    _isApproved = false;
                    //this.createUserList();
                }
                else if ((_xmlReader.NodeType == XmlNodeType.Element) && (_xmlReader.Name == "Supervisor"))
                {
                    this.readSupervisor();
                }
                else if ((_xmlReader.NodeType == XmlNodeType.Element) && (_xmlReader.Name == "Student"))
                {
                    this.readStudent();
                }
            }
            _xmlReader.Close();
        }

        private void createUserList()
        {
            _users=new ArrayList();
            _approvalList = new List<Supervisor>();
        }

        private void readStudent()
        {
            string userid = null;
            string username = null;
            string password = null;
            string email = null;
            bool isActive = true;
            XmlReader subReader = _xmlReader.ReadSubtree();
            while (subReader.Read())
            {
                if (subReader.Name == "UserID")
                {
                    userid = subReader.ReadString();
                }
                else if (subReader.Name == "Username")
                {
                    username = subReader.ReadString();
                }
                else if (subReader.Name == "Password")
                {
                    password = subReader.ReadString();
                }
                else if (subReader.Name == "EMail")
                {
                    email = subReader.ReadString();
                }
                else if (subReader.Name == "Active")
                {
                    if (subReader.ReadString() != "true")
                        isActive = false;
                }
            }

            password = StringEncryption.Decrypt(password, "sharpvtcpwd");

            Student student = new Student(userid, username, password,email,isActive);
            _users.Add(student);
        }

        private void readSupervisor()
        {            
            string userid=null;
            string username=null;
            string password=null;
            string email = null;
            bool isActive = true;
            XmlReader subReader = _xmlReader.ReadSubtree();
            while (subReader.Read())
            {
                if (subReader.Name == "UserID")
                {
                    userid = subReader.ReadString();
                }
                else if (subReader.Name == "Username")
                {
                    username = subReader.ReadString();
                }
                else if (subReader.Name == "Password")
                {
                    password = subReader.ReadString();
                }
                else if (subReader.Name == "EMail")
                {
                    email = subReader.ReadString();
                }
                else if (subReader.Name == "Active")
                {
                    if (subReader.ReadString() != "True")
                        isActive = false;
                }
            }

            password = StringEncryption.Decrypt(password, "sharpvtcpwd");
            Supervisor sup = new Supervisor(userid, username, password,email,isActive);
            // if the supervisor is approved
            if (_isApproved)
                _users.Add(sup);
            else
                _approvalList.Add(sup);
            
        }
        #endregion
    }
}
